package com.example.mymall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mymall.entity.UserEntity;
import com.example.mymall.entity.UserGoodsCartVO;
import com.example.mymall.repository.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author <a href="https://srpihot.site">Srpihot</a>
 * @since 2021-10-06
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping(value = "/findAll", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public List<UserEntity> findAll(){
        return userService.list(null);
    }

    @RequestMapping(value = "/findById", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public UserEntity findById(int id){
        return userService.getById(id);
    }

    @RequestMapping(value = "/resiger", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> resiger(UserEntity user){
        Map<String,Object> res = new HashMap<String,Object>();
        String username = user.getUsername();
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        UserEntity result = userService.getOne(queryWrapper);
        if(result != null){
            res.put("msg","error");
            res.put("data","用户名已被占用~");
        }else{
            try{
              userService.save(user);
              res.put("msg","success");
              res.put("data","注册成功~");
            } catch (Exception e){
                res.put("msg","error");
                res.put("data",e.toString());
            }
        }
        return res;
    }

    @RequestMapping(value = "/page", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public IPage<UserEntity> page(int currentPage, int pageSize){
        return userService.selectUserPage(currentPage,pageSize);
    }

    @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public List<UserGoodsCartVO> page(){
        return userService.test();
    }

    @RequestMapping(value = "/getCartPage", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public IPage<UserGoodsCartVO> pageCartDetail(int currentPage, int pageSize){
        return userService.getCartDetailAll(currentPage,pageSize);
    }

}

